package com.example.datn.activities;

import com.example.datn.R;
import com.example.datn.adapter.TabPagerAdapter;
import com.example.datn.manager.ResourceManager;
import com.example.datn.manager.SongManager;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;

@SuppressLint("NewApi")
public class SongsViewActivity extends FragmentActivity implements TabListener{

	private ViewPager viewPager;
    private TabPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles
    private String[] tabs = { "Song", "Album" };
	
	@Override
	protected void onCreate(Bundle avedInstanceState) {
		super.onCreate(avedInstanceState);		
		setContentView(R.layout.tab_view);
		
		viewPager = (ViewPager)findViewById(R.id.pager);
		actionBar = getActionBar();
        mAdapter = new TabPagerAdapter(getSupportFragmentManager());
        
        viewPager.setAdapter(mAdapter);                   
        actionBar.setTitle("nghe nhac");
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        
        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }
        
        viewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int position) {
				// on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {}
		});              	
	}		
	
	@Override
	public void onBackPressed() {
		killCurrentMediaPlayer();
		super.onBackPressed();
	}


	private void killCurrentMediaPlayer() {
		MediaPlayer mediaPlayer = SongManager.getInstance().getCurrentMediaPlayer();				
		try {
			if(mediaPlayer != null){
				mediaPlayer.stop();
	            mediaPlayer.release();	            
	            SongManager.getInstance().setCurrentMediaPlayer(null);
	            SongManager.getInstance().setCurrentSong(null);
			}
        }
        catch(Exception e) {
            e.printStackTrace();
        }
	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		viewPager.setCurrentItem(tab.getPosition());
	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
		
	}

}
